/*
 * Simplex3dConsole
 * Copyright (C) 2011, Aleksey Nikiforov
 *
 * This file is part of Simplex3dConsole.
 *
 * Simplex3dConsole is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Simplex3dConsole is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package simplex3d.console;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;


/**
 * @author Aleksey Nikiforov (lex)
 */
public class Splash extends JFrame implements ActionListener {

    private Timer timer;
    private int delay = 1000;
    private String feedbackText;
    private int dotCount = 0;


    /** Creates new form Splash */
    public Splash() {
        initComponents();

        timer = new Timer(delay, this);
        timer.setInitialDelay(delay);
        timer.start();
    }


    public void actionPerformed(ActionEvent e) {
        updateFeedbackLabel();
    }

    private String getDots() {
        dotCount += 1;
        if (dotCount > 3) dotCount = 1;

        String res = "";
        for (int i = 0; i < dotCount; i++) {
            res += ".";
        }

        return res;
    }

    private void updateFeedbackLabel() {
        feedbackLabel.setText("Starting Console: " + feedbackText + getDots());
    }

    public void setStatusText(final String text) {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    feedbackText = text;
                    dotCount = 0;
                    updateFeedbackLabel();
                }
            });
        } catch (Exception ignore) {}
    }

    private ImageIcon loadSplash() {
        String path = "simplex3d/console/simplex3d-logo.png";
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);

        try {
            byte[] buff = new byte[1024];
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int len = 0; while (len >= 0) {
                len = in.read(buff);
                if (len > 0) out.write(buff, 0, len);
            }
            in.close();

            return new ImageIcon(out.toByteArray());
        }
        catch (Exception ignore) {
            return null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splashPanel = new javax.swing.JPanel();
        logoLabel = new javax.swing.JLabel(null, loadSplash(), JLabel.CENTER);
        separator = new javax.swing.JSeparator();
        feedbackLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Starting Console");
        setResizable(false);
        setUndecorated(true);

        splashPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(146, 151, 161), 1, true));

        separator.setBorder(null);

        feedbackLabel.setText("Starting Console: rebuilding jars...");
        feedbackLabel.setBorder(null);

        javax.swing.GroupLayout splashPanelLayout = new javax.swing.GroupLayout(splashPanel);
        splashPanel.setLayout(splashPanelLayout);
        splashPanelLayout.setHorizontalGroup(
            splashPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(splashPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(separator, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
            .addGroup(splashPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(feedbackLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                .addContainerGap())
        );
        splashPanelLayout.setVerticalGroup(
            splashPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(splashPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(feedbackLabel)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splashPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splashPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static Splash showSplash() {
        final Splash[] splashRef = new Splash[1];
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    splashRef[0] = new Splash();
                    ConsoleFrame.positionMiddle(splashRef[0]);
                    splashRef[0].setVisible(true);
                }
            });
        } catch (Exception ignore) {}
        return splashRef[0];
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel feedbackLabel;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JSeparator separator;
    private javax.swing.JPanel splashPanel;
    // End of variables declaration//GEN-END:variables

}
